package io.renren.modules.jxgk.controller;

import io.renren.common.annotation.SysLog;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.modules.jxgk.entity.TeacherInfoEntity;
import io.renren.modules.jxgk.service.TeacherInfoService;
import io.renren.modules.jxgkphone.entity.TeacherInfo;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wuwenze.poi.ExcelKit;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("TeacherInfo/")
public class TeacherInfoController {

    @Autowired
    TeacherInfoService teacherInfoService;

    @RequestMapping("Select")
    public R list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        List<TeacherInfoEntity> cse = teacherInfoService.queryList(query);
        int total = teacherInfoService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(cse, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }
    @RequestMapping("/Delete")
    public R delete(@RequestBody String[] indexs){
        teacherInfoService.deleteBatch(indexs);
        return R.ok();
    }
    @RequestMapping("/Insert")
    public R save(@RequestBody TeacherInfoEntity ste){
        teacherInfoService.save(ste);
        return R.ok();
    }
    @RequestMapping("/Info/{index}")
    public R info(@PathVariable("index") String index){
        TeacherInfoEntity cse = teacherInfoService.queryObject(index);
        return R.ok().put("TeacherInfo", cse);
    }
    @SysLog("修改用户")
    @RequestMapping("/Update")
    public R update(@RequestBody TeacherInfoEntity cse){
        teacherInfoService.update(cse);
        return R.ok();
    }
    @RequestMapping("/export")
    public R export(HttpServletResponse response, @RequestParam Map<String, Object> params ) {
        List<TeacherInfoEntity> cse = null;
        try{
            cse =  teacherInfoService.queryList(params);
            ExcelKit.$Export(TeacherInfoEntity.class, response).toExcel(cse, "教师信息汇总");
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        return R.ok();
    }
    @RequestMapping("/getTeacherByCollege")
    public R getTeacherByCollege(HttpServletResponse response, @RequestParam Map<String, Object> params ) {

        String username = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername();
        List<TeacherInfo> teacherInfos = teacherInfoService.getTecahersByCollegeUserName(username);

        return R.ok().put("list", teacherInfos);
    }
}